package code15.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

public class TestTransferStream {

	public static void main(String[] args) {
		//System.out.println("defaultCharset:"+Charset.defaultCharset());
		
		//f1();
		
		//f2();
		
		t1("src/code15/io/b4.txt", "gbk", "src/code15/io/b41.txt", "utf-8");
	}
	/**
	 * 使用指定字符集读取指定文本文件内容, 并使用指定的字符集将读取到的内容保存到指定位置的位置中
	 * @param path1
	 * @param charsetName1
	 * @param path2
	 * @param charsetName2
	 */
	public static void t1(String path1, String charsetName1, String path2, String charsetName2) {
		File f1 = new File(path1);
		if (f1.isFile()) {
			try (
					BufferedReader br = new BufferedReader(
							new InputStreamReader(
									new FileInputStream(f1), 
									charsetName1)
							);
					//PrintWriter pw = new PrintWriter(path2, charsetName2);
					FileOutputStream fos = new FileOutputStream(path2);
					) {
				String line = null;
				while ( (line = br.readLine()) != null) {
					//pw.println(line);
					// 上下两个方式是等效的
					fos.write(line.getBytes(charsetName2));
					fos.write('\n');
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	private static void f2() {
		try (
				// 使用默认字符集(UTF-8)
				OutputStreamWriter osw = new OutputStreamWriter(
						new FileOutputStream("src/code15/io/b3.txt"));
				// 再次包装, 方便使用
				BufferedWriter bw = new BufferedWriter(osw);
				) {
			bw.write("测试测试测试测试测试");
			bw.newLine();
			bw.write("Helloween");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("------------");
		try (
				// 使用 指定字符集 保存文件内容
				OutputStreamWriter osw = new OutputStreamWriter(
						new FileOutputStream("src/code15/io/b4.txt"),
						"GBK");
				// 再次包装, 方便使用
				BufferedWriter bw = new BufferedWriter(osw);
				) {
			bw.write("测试测试测试测试测试");
			bw.newLine();
			bw.write("Helloween");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void f1() {
		try (
				// 使用默认字符集
				InputStreamReader isr = new InputStreamReader(
						new FileInputStream("src/code15/io/b.txt"));
				// 再次包装, 方便使用
				BufferedReader br = new BufferedReader(isr);
				) {
			String line = null;
			while ( (line = br.readLine()) != null ) {
				System.out.println(line);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("-----------");
		try (
				// 使用 指定的字符集, 一般是文件保存时使用的字符集
				InputStreamReader isr = new InputStreamReader(
						new FileInputStream("src/code15/io/b2.txt"),
						"GBK");
				// 再次包装, 方便使用
				BufferedReader br = new BufferedReader(isr);
				) {
			String line = null;
			while ( (line = br.readLine()) != null ) {
				System.out.println(line);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
